$(function () {
    $('#login-box a').on('click',function(){
        $('#login-box').hide()
        $('#reg-box').show()
    })
    $('#reg-box a').on('click',function(){
        $('#login-box').show()
        $('#reg-box').hide()
    })

    //正则判断
    const form = layui.form
    form.verify({
        username:[
            /^[a-zA-Z0-9]{1,10}$/,
            "用户名必须1到10位数字和字母"
        ],
        password:[
            /^[a-zA-Z0-9]{6,12}$/,
            "密码必须6到12位数字和字母"
        ],

        //确认密码校验规则,密码和确认密码一致
        rePwd:function(value){
            //判断 ：密码和确认密码不一致，则报错
            let v1 = $('#reg-box input[name=password]').val()
            if(value != v1){
                return "两次密码输入不一致！"
            }
        }});

    $('')

    //注册事件
    $('#resform').on('submit',function(e){
        e.preventDefault()
        axios({
            url:'/api/reguser',
            method:'post',
            data:$(this).serialize(),
        }).then(({data:res})=>{
            //成功回调
            if(res.status != 0 ) return layer.msg(res.message)
            layer.msg('恭喜您，注册成功！')
            $('#resform')[0].reset()
            $('#reg-box a').click()
        });
    })

    //登录事件
    $('#login-form').on('submit',function(e){
        e.preventDefault()
        console.log($(this).serialize())
        axios({
            url:'/api/login',
            method:'POST',
            data: $(this).serialize()
        }).then(({data:res})=>{
           
            if(res.status != 0 ) return layer.msg(res.message)
            localStorage.setItem('token',res.token)
            location.href = '/index.html'
        });
    })
})